#include "Date.h"

//Date::Date(Date* const this int year, int month, int day)
Date::Date(int year, int month, int day)
{
	if (month > 0 && month <= 12 && day > 0 && day <= GetMonthday(year, month))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		cout << "非法日期" << endl;
	}
}

bool Date::operator<(const Date& d) const
{
	if (_year < d._year)
	{
		return true;
	}
	else if (_year == d._year && _month < d._month)
	{
		return true;
	}
	else if (_year == d._year && _month == d._month && _day < d._day)
	{
		return true;
	}
	else
		return false;
}

bool Date::operator==(const Date& d) const
{
	return _year == d._year && _month == d._month && _day == d._day;
}

bool Date::operator<=(const Date& d) const
{
	return (*this < d) || (*this == d);
}

bool Date::operator>=(const Date& d) const
{
	return !(*this < d);
}

bool Date::operator>(const Date& d) const
{
	return !(*this <= d);
}

bool Date::operator!=(const Date& d) const
{
	return !(*this == d);
}

int Date::GetMonthday(int year, int month)
{
	static int datearray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (month == 2 && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)))
	{
		return 29;
	}

	return datearray[month];

}

//+=运算符重载实现+
Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		*this = *this -= -day;
		return *this;
	}
	_day += day;
	while (_day > GetMonthday(_year, _month))
	{
		_day -= GetMonthday(_year, _month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}

	return *this;
}

Date Date::operator+(int day) const
{
	Date temp(*this);
	temp += day;
	return temp;;
}

//+运算符重载函数实现+=
//Date Date::operator+(int day)
//{
//	Date temp(*this);
//	//temp += day;
//	temp._day += day;
//	while (temp._day > GetMonth(temp._year, temp._month))
//	{
//		temp._day -= GetMonth(temp._year, temp._month);
//		temp._month++;
//		if (temp._month == 13)
//		{
//			temp._year++;
//			temp._month = 1;
//		}
//	}
//
//	return temp;;
//}

//Date& Date::operator+=(int day)
//{
//	*this=*this + day;
//	return *this;
//}

Date& Date:: operator-=(int day)
{
	if (day < 0)
	{
		*this = *this += -day;
		return *this;
	}
	_day -= day;
	while (_day <= 0)
	{
		_day += GetMonthday(_year, _month);
		--_month;
		if (_month == 0)
		{
			_month = 12;
			_year--;
		}
	}
	return *this;

}
Date Date::operator-(int day) const
{
	Date temp(*this);
	temp -= day;
	return temp;

}

//不需要额外开辟空间
Date& Date::operator++()
{
	*this += 1;
	return *this;
}

//需要额外开辟空间
Date Date::operator++(int)
{
	Date temp = *this;
	*this += 1;
	return temp;
}

Date& Date::operator--()
{
	*this -= 1;
	return *this;
}

Date Date::operator--(int)
{
	Date temp = *this;
	*this -= 1;
	return temp;
}

int Date::operator-(const Date& d) const
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int count = 0;
	while (min != max)
	{
		++min;
		count++;
	}

	return flag * count;
}

void Date::operator<<(ostream& out)
{
	out << _year << "年" << _month << "月" << _day << "日" << endl;
}

//引用返回cout的类 实现连续输出
//cout<<内置类型是在库里面实现的  自动识别类型是由运算符重载函数构成重载实现的
ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}

istream& operator>>(istream& in, Date& d)
{
	int year, month, day;
	in >> year >> month >> day;
	if (month > 0 && month <= 12 && day > 0 && day <= d.GetMonthday(year, month))
	{
		d._year = year;
		d._month = month;
		d._day = day;
	}
	else
	{
		cout << "非法日期" << endl;
	}
	return in;
}

